package cn.android666.logfilter

import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import kotlin.test.Test


class LogReaderTest {

    /** 截止上传成功的GPS，只保留经纬度 */
    @Test
    fun readGPSLogTest2() {
        val path = "D:\\360MoveData\\Users\\Even\\Desktop\\AndroidLog\\17010720"
        val fileName = "log.txt"
        val contain = "上传GPS("
        val logFile = File(path, fileName)
        val newFile = File(path, "GPS.txt")
        val writer = BufferedWriter(FileWriter(newFile))
        LogReader.readLine(logFile, contain) { line ->
            val start = line.indexOf("(") + 1
            val end = line.indexOf(")")
            writer.write(line.substring(start, end))
            writer.newLine()
        }
        writer.close()
    }

    /** 读GPS日志测试 */
    @Test
    fun readGPSLogTest() {
        val path = "D:\\360MoveData\\Users\\Even\\Desktop\\AndroidLog\\17010720"
        val fileName = "log_2025_06_09.txt"
        val contain = "timeOk = false"
        val logFile = File(path, fileName)
        val newFile = File(path, "${contain}.txt")
        val writer = BufferedWriter(FileWriter(newFile))
        LogReader.readLine(logFile, contain) { line ->
            writer.write(line)
            writer.newLine()
        }
        writer.close()
    }

    @Test
    fun test() {
        val path = "D:\\360MoveData\\Users\\Even\\Desktop\\AndroidLog"
        val fileName = "log.txt"
        val logFile = File(path, fileName)
        LogReader.readLine(logFile, "LoginPresenter") { line ->
            println(line)
        }
    }

    /** 读sim卡信号日志测试 */
    @Test
    fun readSimSignalLogTest() {
        val path = "D:\\360MoveData\\Users\\Even\\Desktop\\AndroidLog"
        val fileName = "log_2025_05_07.txt"
        val contain = "PhoneStateListenerImpl"
        val logFile = File(path, fileName)
        val newFile = File(path, "${contain}.txt")
        val writer = BufferedWriter(FileWriter(newFile))
        LogReader.readLine(logFile, contain) { line ->
            writer.write(line)
            writer.newLine()
        }
        writer.close()
    }

    /** 读ping日志测试 */
    @Test
    fun readPingLogTest() {
        val path = "D:\\360MoveData\\Users\\Even\\Desktop\\AndroidLog"
        val fileName = "log_2025_01_08.txt"
        val logFile = File(path, fileName)
        LogReader.readLine(logFile, "NetworkConnectionCheckEngine") { line ->
            println(line)
        }
    }

    /** 把日志过滤后写入文件 */
    @Test
    fun writeFilterLogToFileTest() {
        val path = "D:\\360MoveData\\Users\\Even\\Desktop\\AndroidLog"
        val fileName = "log_2025_03_10.txt"
        val contain = "心跳请求失败"
        val logFile = File(path, fileName)
        val newFile = File(path, "${contain}.txt")
        val writer = BufferedWriter(FileWriter(newFile))
        LogReader.readLine(logFile, contain) { line ->
            writer.write(line)
            writer.newLine()
        }
        writer.close()
    }

    @Test
    fun writeFilterLogToFileTest2() {
        val path = "D:\\360MoveData\\Users\\Even\\Desktop\\AndroidLog"
        val fileName = "log.txt"
        val excludeText = "CheckMultiNetworkService"
        val logFile = File(path, fileName)
        val newFile = File(path, "${excludeText}.txt")
        val writer = BufferedWriter(FileWriter(newFile))
        LogReader.readLineExclude(logFile, excludeText) { line ->
            writer.write(line)
            writer.newLine()
        }
        writer.close()
    }
}